package com.authentication.code2Token.impl;

import com.authentication.code2Token.RefreshTokenRepository;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenContext;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InMemoryRefreshTokenRepository implements RefreshTokenRepository {

    Map<String, OAuth2TokenContext> map = new ConcurrentHashMap<String, OAuth2TokenContext>();

    @Override
    public void save(String refreshToken, OAuth2TokenContext oAuth2TokenContext) {
        map.put(refreshToken,oAuth2TokenContext);
        if (oAuth2TokenContext.getRegisteredClient() !=null) {
            Duration refreshTokenTimeToLive = oAuth2TokenContext.getRegisteredClient().getTokenSettings().getRefreshTokenTimeToLive();
//            TODO 超时之后 删除 此项
        }
    }

    @Override
    public OAuth2TokenContext get(String refreshToken) {
        return map.get(refreshToken);
    }
}
